﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RadarMonitor.ViewModel;
using RadarMonitor.CIMISS;
using RadarMonitor.StatusMonitor;
using RadarMonitor.SettingPage;
using RadarMonitor.FTPMonitor;
using Timer = System.Windows.Forms.Timer;
using RadarMonitor.DB;

namespace RadarMonitor
{


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private LogCardViewModel logCardViewModel = new LogCardViewModel();
        private BaseFTPViewModel baseFTPViewModel = new BaseFTPViewModel();
        private ProductsMonitorViewModel productsMonitorViewModel = new ProductsMonitorViewModel();
        private NetMonitorViewModel netMonitorViewModel = new NetMonitorViewModel();
        private StatusMonitorViewModel statusMonitorViewModel = new StatusMonitorViewModel();
        private CIMISSViewModel cIMISSViewModel = new CIMISSViewModel();
        private Timer timer;

        public MainWindow()
        {
            InitializeComponent();

            BaseFTPCard.DataContext = baseFTPViewModel;
            if (RadarMonitor.Setting.Setting.GetConfig().baseFTP.AutoStart)
            {
                baseFTPViewModel.start();
            }

            ProductsMonitorCard.DataContext = productsMonitorViewModel;
            if (RadarMonitor.Setting.Setting.GetConfig().productFTP.AutoStart)
            {
                productsMonitorViewModel.start();
            }

            CimissCard.DataContext = cIMISSViewModel;
            if (RadarMonitor.Setting.Setting.GetConfig().CIMISS.AutoStart)
            {
                cIMISSViewModel.start();
            }

            PingCard.DataContext = netMonitorViewModel;
            if (RadarMonitor.Setting.Setting.GetConfig().ping.AutoStart)
            {
                netMonitorViewModel.start();
            }

            StatusMonitorCard.DataContext = statusMonitorViewModel;
            if (RadarMonitor.Setting.Setting.GetConfig().status.AutoStart)
            {
                statusMonitorViewModel.start();
            }
            RecordCard.DataContext = logCardViewModel;

            Timer_Tick(null, null);
            timer = new Timer();
            timer.Interval = 60*1000 * 5;
            timer.Tick += Timer_Tick;
            timer.Enabled = true;
            

        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            //
            try
            {
                var record = DBHelper.Instance.TodaylastedStatusAlarmRecord();
                statusMonitorViewModel.LastRecord = record;
            }
            catch { }
            
        }

        private void logItemsUpdate()
        {
            logCardViewModel.update();
        }

        #region 日志翻页
        private void PageNumTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                var num= int.Parse(PageNumTB.Text);
                logCardViewModel.gotoPage(num);
                PageNumTB.Text =  logCardViewModel.PageNum.ToString();
            }
            catch { }
        }
        /// <summary>
        /// 回车键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageNumTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                try
                {
                    var num = int.Parse(PageNumTB.Text);
                    logCardViewModel.gotoPage(num);
                    PageNumTB.Text = logCardViewModel.PageNum.ToString();
                }
                catch { }
            }
        }

        private void priorLogPage(object sender, RoutedEventArgs e)
        {
            var num = logCardViewModel.PageNum - 1;
            logCardViewModel.gotoPage(num);
        }

        private void nextLogPage(object sender, RoutedEventArgs e)
        {
            var num = logCardViewModel.PageNum + 1;
            logCardViewModel.gotoPage(num);
        }
        #endregion
        #region 标题栏
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void MinimizeWindow_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void CloseWindow_Close(object sender, RoutedEventArgs e)
        {
            Close();
            Application.Current.Shutdown();
            System.Environment.Exit(0);
        }

        #endregion

        private void ShowRecords(object sender, RoutedEventArgs e)
        {
            if (sender is Button btn)
            {
                var recordWin = new ShowRecordsWindows()
                {
                    Owner = this,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    DataContext = logCardViewModel
                };

                if (btn.Tag.Equals("BaseFTP"))
                {
                    logCardViewModel.sourceFilter = 1;
                }

                if (btn.Tag.Equals("CIMISS"))
                {
                    logCardViewModel.sourceFilter =2;
                }

                if (btn.Tag.Equals("ProductFTP"))
                {
                    logCardViewModel.sourceFilter = 3;
                }

                if (btn.Tag.Equals("RDAMonitor"))
                {
                    logCardViewModel.sourceFilter = 5;
                }

                if (btn.Tag.Equals("Ping"))
                {
                    logCardViewModel.sourceFilter = 4;
                }

                recordWin.ShowDialog();
            }
        }

        private void StartOrStop(object sender, RoutedEventArgs e)
        {
            if(sender is Button btn)
            {
                if (btn.Tag.Equals("BaseFTP")){
                    baseFTPViewModel.toogle();
                }

                if (btn.Tag.Equals("CIMISS")){
                    cIMISSViewModel.toogle();
                }
                
                if (btn.Tag.Equals("ProductFTP")){
                    productsMonitorViewModel.toogle();
                }
                
                if (btn.Tag.Equals("RDAMonitor")){
                    statusMonitorViewModel.toogle();
                }
                
                if (btn.Tag.Equals("Ping")){
                    netMonitorViewModel.toogle();
                }
            }
        }

        private void OpenCardSetting(object sender, RoutedEventArgs e)
        {
            if (sender is Button btn)
            {
                Window settingWin = new CardConfigWindow()
                {
                    Owner = this
                };
                if (btn.Tag.Equals("BaseFTP"))
                {
                    settingWin = new BaseFTPConfigWindow()
                    {
                        Owner = this,
                        monitor= BaseFtpMonitor.Instance.monitor,
                    };
                    settingWin.Title = "雷达基数据传输监控(FTP)";
                    settingWin.DataContext = RadarMonitor.Setting.Setting.GetConfig().baseFTP;
                    
                }

                if (btn.Tag.Equals("CIMISS"))
                {
                    settingWin = new CMIISSConfigWindow()
                    {
                        Owner = this
                    };
                    settingWin.Title = "雷达基数据流传输监控(CIMISS)";
                    settingWin.DataContext = RadarMonitor.Setting.Setting.GetConfig().CIMISS;
                }

                if (btn.Tag.Equals("ProductFTP"))
                {
                    settingWin = new BaseFTPConfigWindow()
                    {
                        Owner = this,
                        monitor = ProductsMonitor.Instance.monitor,
                    };
                    settingWin.Title = "雷达产品传输监控";
                    settingWin.DataContext = RadarMonitor.Setting.Setting.GetConfig().productFTP;
                }

                if (btn.Tag.Equals("RDAMonitor"))
                {
                    settingWin.Title = "雷达站网络监控";
                    settingWin.DataContext = RadarMonitor.Setting.Setting.GetConfig().status;
                }

                if (btn.Tag.Equals("Ping"))
                {
                    settingWin.Title = "雷达状态信息监控";
                    settingWin.DataContext = RadarMonitor.Setting.Setting.GetConfig().ping;
                }
                settingWin.Show();
            }

        }

        private void refreshRecords(object sender, RoutedEventArgs e)
        {
            logCardViewModel.refresh();
        }

        private void showAllRecored_Click(object sender, RoutedEventArgs e)
        {
            var recordWin = new ShowRecordsWindows()
            {
                Owner = this,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                DataContext = logCardViewModel
            };

            recordWin.ShowDialog();

        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is ListBox listBox)
            {
                if (listBox.SelectedIndex ==1)
                {
                    var about = new AboutWin()
                    {
                        Owner = this,
                        WindowStartupLocation = WindowStartupLocation.CenterOwner
                    };
                    about.ShowDialog();
                }
                if(listBox.SelectedIndex == 0)
                {
                    var setting = new GlobalSetting()
                    {
                        Owner = this,
                        WindowStartupLocation = WindowStartupLocation.CenterOwner
                    };
                    setting.ShowDialog();
                }

                listBox.SelectedItem = null;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            timer.Enabled = false;
            timer.Stop();
        }

        private void soundMsgBtn_Click(object sender, RoutedEventArgs e)
        {
            Alarm.AudioHandler.Instance.cleanAlarm();
        }
    }
}
